/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.interpreter.backends.rete;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;

/**
 *
 * @author Laurent WOUTERS
 */
public class AlphaMemory implements FactActivable, FactHolder {
    private Collection<TripleFact> facts;
    private LinkedList<FactActivable> children;
    
    @Override public Collection<TripleFact> getFacts() { return facts; }
    @Override public void addChild(FactActivable node) { children.addFirst(node); }
    @Override public void removeChild(FactActivable node) { children.remove(node); }
    
    public AlphaMemory() {
        facts = new ArrayList<TripleFact>();
        children = new LinkedList<FactActivable>();
    }
    
    @Override public void activateFact(TripleFact fact) {
        facts.add(fact);
        for (FactActivable child : children)
            child.activateFact(fact);
    }
    @Override public void deactivateFact(TripleFact fact) {
        facts.remove(fact);
        for (FactActivable child : children)
            child.deactivateFact(fact);
    }
    @Override public void activateFacts(Collection<TripleFact> facts) {
        this.facts.addAll(facts);
        for (FactActivable child : children)
            child.activateFacts(new FastBuffer<TripleFact>(facts));
    }
    @Override public void deactivateFacts(Collection<TripleFact> facts) {
        this.facts.removeAll(facts);
        for (FactActivable child : children)
            child.deactivateFacts(new FastBuffer<TripleFact>(facts));
    }
}
